package com.balabala.result;

import com.balabala.gym_enum.ExceptionEnum;
import com.balabala.gym_enum.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 *
 * @author 何涛
 * @date 2023/4/28 0028
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    public Result(T data, String msg, Integer code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    private T data;
    private String msg;
    private Long count;
    private Integer code;

    public static Result ok(ResultEnum resultEnum) {

        return ok(resultEnum.getCode(), resultEnum.getMsg());
    }
    public static Result ok(ResultEnum resultEnum, Object data) {
        Result ok = ok(resultEnum);
        ok.data = data;
        return ok;
    }

    public static <T> Result ok(T data, Long count) {
        Result ok = ok();
        ok.setData(data);
        ok.setCount(count);
        return ok;
    }

    public static <T> Result ok(T data) {
        return new Result<T>(data, ResultEnum.GENERALLY_SUCCESS.getMsg(), ResultEnum.GENERALLY_SUCCESS.getCode());
    }

    public static <T> Result ok(Integer code, String message) {
        return new Result(message, code);
    }

    public static Result ok() {
        return new Result(null, "success", 0);
    }

    public static Result fail() {
        return new Result(null, "fail", 444);
    }

    public static Result fail(Integer code) {
        Result fail = fail();
        fail.setCode(code);
        return fail;
    }

    public static Result fail(Integer code, String msg) {
        Result fail = fail(code);
        fail.msg = msg;
        return fail;
    }

    public static Result fail(ExceptionEnum exceptionEnum) {
        return fail(exceptionEnum.getCode(), exceptionEnum.getMsg());
    }


    public static Result judge(Boolean b) {
        return b ? Result.ok(ResultEnum.GENERALLY_SUCCESS) : Result.fail(ExceptionEnum.GENERALLY_FAIL);
    }

    public Result(String msg, Integer code) {
        this.msg = msg;
        this.code = code;
    }


    public static Result fail(ResultEnum generallyFail) {
        return fail(generallyFail.getCode(),generallyFail.getMsg());
    }
}
